﻿/*jslint browser: true, cap: false, passfail: false, undef: false, white: false */
/*global SONGHAY YAHOO */

(function(){

    //Module-level members:
    var application = SONGHAY.ui.GenericWebEditor;
    var client = SONGHAY.ui.Client;

    SONGHAY.ui.GenericWebEditor.clickHandler =
    {
        calendarShow: function(e)
        {
            YAHOO.util.Event.stopEvent(e);

            //Look for pass-through object from YAHOO.util.Event.addListener():
            var oEventPassThrough = (arguments.length > 1) ? arguments[1] : null;

            if(oEventPassThrough && oEventPassThrough.sourceId && oEventPassThrough.targetId){} else { return; }

            var targetId = oEventPassThrough.targetId;
            var sourceId = oEventPassThrough.sourceId;

            application.calendarPickSource = YAHOO.util.Dom.get(sourceId);
            application.calendarPickTarget = YAHOO.util.Dom.get(targetId);

            var oPosition = YAHOO.util.Dom.getXY(application.calendarPickSource);

            application.calendar.oDomContainer.style.display='block';

            YAHOO.util.Dom.setXY(application.calendar.oDomContainer, 
                [oPosition[0],oPosition[1]+application.calendarPickSource.offsetHeight+1]);
        },

        searchPageBack: function(e)
        {
            client.ui.TablePageViewer.pageBack('SearchResultsTable', 'NavigationCaption');
        },

        searchPageForward: function(e)
        {
            client.ui.TablePageViewer.pageForward('SearchResultsTable', 'NavigationCaption');
        },

        toggleIndexBlog: function(e)
        {
            YAHOO.util.Event.preventDefault(e);

            var o = YAHOO.util.Event.getTarget(e);
            var oParent = null;

            if(o.src && o.parentNode) { oParent = o.parentNode; }

            var vID; var vStyle; var oElements;
            if(oParent && oParent.id)
            {//Probably IE:
                vID = oParent.id.replace('WebIndexTogAnchor','WebIndexBlock');
                vStyle = YAHOO.util.Dom.getStyle(vID,'display');
                vStyle = (vStyle.toLowerCase() == 'none') ? 'block' : 'none';
                o.src = (vStyle == 'none') ? './js/songhay/assets/cube-plus.gif' : './js/songhay/assets/cube-minus.gif';
                YAHOO.util.Dom.setStyle(vID,'display',vStyle);
            }
            else if(o && o.href)
            {//Probably Firefox:
                vID = o.id.replace('WebIndexTogAnchor','WebIndexBlock');
                vStyle = YAHOO.util.Dom.getStyle(vID,'display');
                vStyle = (vStyle.toLowerCase() == 'none') ? 'block' : 'none';
                oElements = YAHOO.util.Dom.getElementsBy(function(e){ return (e.tagName.toLowerCase() == 'img'); },'img',o);
                if(oElements && (oElements.length == 1)) { oElements[0].src = (vStyle == 'none') ? './js/songhay/assets/cube-plus.gif' : './js/songhay/assets/cube-minus.gif'; }
                YAHOO.util.Dom.setStyle(vID,'display',vStyle);
            }
        },

        ulDropDownCancelClick: function(e)
        {
            YAHOO.util.Event.stopEvent(e);

            //Look for pass-through object from YAHOO.util.Event.addListener():
            var oEventPassThrough = (arguments.length > 1) ? arguments[1] : null;
            if(oEventPassThrough && oEventPassThrough.targetId){} else { return; }

            var o = null;
            var vTargetId = oEventPassThrough.targetId;

            //Show drop-down items:
            o = YAHOO.util.Dom.get(vTargetId);
            YAHOO.util.Dom.setStyle(o,'display','none');
        },

        ulDropDownClick: function(e)
        {
            YAHOO.util.Event.stopEvent(e);

            //Look for pass-through object from YAHOO.util.Event.addListener():
            var oEventPassThrough = (arguments.length > 1) ? arguments[1] : null;
            if(oEventPassThrough && oEventPassThrough.targetId){} else { return; }

            var vTargetId = oEventPassThrough.targetId;

            //Show drop-down items:
            var oDropDown = YAHOO.util.Dom.get(vTargetId + 'ItemsBlock');
            YAHOO.util.Dom.setStyle(oDropDown,'display','block');
        },

        ulDropDownItemClick: function(e)
        {
            YAHOO.util.Event.stopEvent(e);
            var oTarget = YAHOO.util.Event.getTarget(e);
            if(oTarget) {} else { return; }

            //Look for pass-through object from YAHOO.util.Event.addListener():
            var oEventPassThrough = (arguments.length > 1) ? arguments[1] : null;
            if(oEventPassThrough && oEventPassThrough.targetId){} else { return; }

            var o = null;
            var vTargetId = oEventPassThrough.targetId;

            o = YAHOO.util.Dom.get(vTargetId + 'SelectedItem');
            if(o) { o.innerHTML = oTarget.innerHTML; }

            o = YAHOO.util.Dom.get(vTargetId);
            if(o)
            {
                //CONVENTION: look for target IDs with pattern item_#_#:
                o.value = (oTarget.id.indexOf('item_') != -1) ? oTarget.id.replace(/item_.+_/,'') : oTarget.id;
            }

            //Hide drop-down items:
            var oDropDown = YAHOO.util.Dom.get(vTargetId + 'ItemsBlock');
            YAHOO.util.Dom.setStyle(oDropDown,'display','none');
        },

        ulDropDownRemoveClick: function(e)
        {
            YAHOO.util.Event.stopEvent(e);

            //Look for pass-through object from YAHOO.util.Event.addListener():
            var oEventPassThrough = (arguments.length > 1) ? arguments[1] : null;
            if(oEventPassThrough && oEventPassThrough.targetId){} else { return; }

            var o = null;
            var oDropDown = null;
            var vTargetId = oEventPassThrough.targetId;

            oDropDown = YAHOO.util.Dom.get(vTargetId + 'UlDropDown');
            if(oDropDown) {} else { return; }

            o = YAHOO.util.Dom.get(vTargetId);
            if(o && o.value) { o.value = ''; }

            o = YAHOO.util.Dom.get(vTargetId + 'SelectedItem');
            if (o) { o.innerHTML = '[Not Specified]'; }

            //Hide drop-down items:
            oDropDown = YAHOO.util.Dom.get(vTargetId + 'ItemsBlock');
            YAHOO.util.Dom.setStyle(oDropDown,'display','none');
        },

        ulDropDownResetClick: function(e)
        {
            YAHOO.util.Event.stopEvent(e);

            //Look for pass-through object from YAHOO.util.Event.addListener():
            var oEventPassThrough = (arguments.length > 1) ? arguments[1] : null;
            if(oEventPassThrough && oEventPassThrough.targetId){} else { return; }

            var o = null;
            var oDropDown = null;
            var vTargetId = oEventPassThrough.targetId;

            oDropDown = YAHOO.util.Dom.get(vTargetId + 'UlDropDown');
            if(oDropDown) {} else { return; }

            o = YAHOO.util.Dom.get(vTargetId);
            if(o && o.value) { o.value = oDropDown.initialValue; }

            o = YAHOO.util.Dom.get(vTargetId + 'SelectedItem');
            if(o) { o.innerHTML = oDropDown.initialDisplayName; }

            //Hide drop-down items:
            oDropDown = YAHOO.util.Dom.get(vTargetId + 'ItemsBlock');
            YAHOO.util.Dom.setStyle(oDropDown,'display','none');
        }
    };

})();
